package com.xiao.rabbitmq.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 生产者
 */
public class Producer {
    public static void main(String[] args) throws Exception{
        //设置一个工厂类，进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.94.131");
        connectionFactory.setPort(5672);
        //设置虚拟主机
        connectionFactory.setVirtualHost("/");
        connectionFactory.setConnectionTimeout(60000);
        //创建一个连接
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        System.out.println("开始生产数据");
        for(int i=0; i<5; i++){
            String msg = "Hello RabbitMq";
            //1 exchange   2 routingKey
            //如果exchange为空，则使用默认的exchange，
            //它的路由规则时routingkey得和消费队列相同
            channel.basicPublish("", "test001", null, msg.getBytes());
        }
        channel.close();
        connection.close();
    }
}
